<?php
require_once 'BaseMySQLDAO.php';

class AppDAO extends BaseMySQLDAO {

    public function __construct($context) {
        parent::__construct($context, 'app');
    }

    public function getAllApp($start, $limit) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "	SELECT app.*, game_category.name as parent 
            			FROM app, game_category 
            			WHERE game_category.id = app.parent 
            			ORDER BY `id` DESC
            			LIMIT :start, :limit
            		";
            $param = array(':start' => $start, ':limit' => $limit);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
	
    public function getApp() {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "	SELECT * 
            			FROM app
            			ORDER BY `name` DESC
            		";
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
 	
    public function countApp() {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "SELECT COUNT(id) as total FROM app";
            $param = array();
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadOneRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result['total'];
    }  

    public function getAppByStatus($status, $start, $limit) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "	SELECT app.*, game_category.name as parent
            			FROM app, game_category 
            			WHERE 	game_category.id = app.parent  
            				AND app.status = :status 
            			ORDER BY `order` ASC 
            			LIMIT :start, :limit
            		";
            $param = array(':status' => $status, ':start' => $start, ':limit' => $limit);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
    
	public function getAppNameByStatus($status) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "	SELECT app.*, game_category.name as parent
            			FROM app, game_category 
            			WHERE 	game_category.id = app.parent  
            				AND app.status = :status 
            			ORDER BY `order` ASC
            		";
            $param = array(':status' => $status);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }

    public function countAppByStatus($status) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "SELECT COUNT(id) as total FROM app WHERE status = :status";
            $param = array(':status' => $status);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadOneRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result['total'];
    }

    public function getAppById($id) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "SELECT * FROM app WHERE id = :id";
            $param = array(':id' => $id);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadOneRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
    
	public function getAllAppByName($name) {
        $result = array();
        try {
            $this -> getContext() -> getTransaction() -> startTransaction();

            $inSQL = "	SELECT app.*, game_category.name as parent
            			FROM app, game_category 
            			WHERE 	game_category.id = app.parent 
           				AND app.id = :name";
            $param = array(':name' => $name);
            $result = $this -> getContext() -> getTransaction() -> getMySQLConnection() -> loadManyRow($inSQL, $param);

            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
        }
        return $result;
    }
    
	public function insertApp($param) {
        $result = null;
        try {
            $inSQL = "INSERT INTO
                        app(id, name, mediumName, SEO_Name, smallImage, mediumImage, largeImage, shortDetail, mediumDetail, longDetail, parent, SEO_title, SEO_metaDescription, SEO_keyword, created_date, lastUpdate_date, status, linkMWork)
                        VALUES (
                           :id, :name, :mediumName, :SEO_Name, :smallImage, :mediumImage, :largeImage, :shortDetail, :mediumDetail, :longDetail, :parent, :SEO_title, :SEO_metaDescription, :SEO_keyword, :created_date, :lastUpdate_date, :status, :linkMWork
                        )";
            $queryParam = array(
                                ':id' => NULL,
                                ':name' => $param['name'],
                                ':mediumName' => $param['mediumName'],
                                ':SEO_Name' => $param['SEO_Name'],
                                ':smallImage' => $param['smallImage'],
                                ':mediumImage' => $param['mediumImage'],
                                ':largeImage' => $param['largeImage'],
                                ':shortDetail' => $param['shortDetail'],
                                ':mediumDetail' => $param['mediumDetail'],
                                ':longDetail' => $param['longDetail'],
                                ':parent' => $param['parent'],
                                ':SEO_title' => $param['SEO_title'],
                                ':SEO_metaDescription' => $param['SEO_metaDescription'],
                                ':SEO_keyword' => $param['SEO_keyword'],
                                ':created_date' => date("Y-m-d h:i:s") ,
                                ':lastUpdate_date' => date("Y-m-d h:i:s"),
                                ':status' => $param['status'],
                                ':linkMWork' => $param['linkMWork']
                            );
            $this -> getContext() -> getTransaction() -> startTransaction('thanhphogame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('mobile');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('choigamemobi');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('mymobigame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('ngoisaogamethu');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

        } catch (Exception $exception) {
            print_r($exception);
            exit;
        }
        return $result;
    }
    
	public function updateApp($param) {
        $result = null;
        try {
            $inSQL = "UPDATE app SET
                                    name = :name,
                                    mediumName = :mediumName,
                                    SEO_Name = :SEO_Name,
                                    smallImage = :smallImage,
                                    mediumImage = :mediumImage,
                                    largeImage = :largeImage,
                                    shortDetail = :shortDetail,
                                    mediumDetail = :mediumDetail,
                                    longDetail = :longDetail,
                                    parent = :parent,
                                    SEO_title = :SEO_title,
                                    SEO_metaDescription = :SEO_metaDescription,
                                    SEO_keyword = :SEO_keyword,
                                    lastUpdate_date = :lastUpdate_date,
                                    status = :status,
                                    `order` = :order,
                                    linkMWork = :linkMWork
                        WHERE id = :id
                        ";
            $queryParam = array(
                                ':id' => $param['id'],
                                ':name' => $param['name'],
                                ':mediumName' => $param['mediumName'],
                                ':SEO_Name' => $param['SEO_Name'],
                                ':smallImage' => $param['smallImage'],
                                ':mediumImage' => $param['mediumImage'],
                                ':largeImage' => $param['largeImage'],
                                ':shortDetail' => $param['shortDetail'],
                                ':mediumDetail' => $param['mediumDetail'],
                                ':longDetail' => $param['longDetail'],
                                ':parent' => $param['parent'],
                                ':SEO_title' => $param['SEO_title'],
                                ':SEO_metaDescription' => $param['SEO_metaDescription'],
                                ':SEO_keyword' => $param['SEO_keyword'],
            					':lastUpdate_date' => date("Y-m-d h:i:s"),
                                ':status' => $param['status'],
                                ':order' => $param['order'],
                                ':linkMWork' => $param['linkMWork']
                            );
            $this -> getContext() -> getTransaction() -> startTransaction('thanhphogame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('mobile');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('choigamemobi');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('mymobigame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('ngoisaogamethu');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
        } catch (Exception $exception) {
            print_r($exception);
            exit;
        }
        return $result;
    }
    
	public function deleteApp($id) {
        try {

            $inSQL = "   DELETE FROM app WHERE id = :id ";
            $queryParam = array(':id' => $id);
            
            $this -> getContext() -> getTransaction() -> startTransaction('thanhphogame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
			
            $this -> getContext() -> getTransaction() -> startTransaction('mobile');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('choigamemobi');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();

            $this -> getContext() -> getTransaction() -> startTransaction('mymobigame');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
            
            $this -> getContext() -> getTransaction() -> startTransaction('ngoisaogamethu');
            $this -> getContext() -> getTransaction() -> getMySQLConnection() -> executeNonQuery($inSQL, $queryParam);
            $this -> getContext() -> getTransaction() -> closeTransaction();
        } catch (Exception $exception) {
            $this -> getContext() -> logException($exception);
            return false;
        }
        return true;
    }
}
?>